//
//  ComposeTextView.swift
//  weibo
//
//  Created by 陈勇 on 16/3/7.
//  Copyright © 2016年 chenyong. All rights reserved.
//

import UIKit
//微博文本视图
class ComposeTextView: UITextView {
    
    // MARK: - 属性
    /// 占位文本
    var placeholder: String? {
        didSet {
            placeholderLabel.text = placeholder
        }
    }
    
    /// 最大文本长度
    var maxLength: Int = 0 {
        didSet {
            tipLabel.text = maxLength.description
        }
    }
    
    override var font: UIFont? {
        didSet {
            placeholderLabel.font = font
        }
    }

    /// 文本，外部调用方一旦设置了文本，需要隐藏占位标签
    override var text: String! {
        didSet {
            placeholderLabel.hidden = hasText()
        }
    }
    
    // MARK: - 构造函数
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
        
        // 监听文本变化通知
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: "textDidChanged",
            name: UITextViewTextDidChangeNotification,
            object: nil)
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 公共方法
    /// 相对 view 来修改提示标签的约束
    func updateTipLabelConstraints(view: UIView){
    
        let margin: CGFloat = 5
        // 更新约束
        // masory make: 创建约束 / update: 更新(已经存在的)约束
        // remake 重建约束(删除之前的`所有`约束，重新 make)
        tipLabel.snp_remakeConstraints { (make) -> Void in
            
            make.right.equalTo(self).offset(-margin)
            make.bottom.equalTo(view.snp_top).offset(-margin)
        }
    }
    // MARK: - 监听方法
    @objc private func textDidChanged() {
        placeholderLabel.hidden = hasText()
        
        let len = maxLength - text.characters.count
        tipLabel.text = len.description
        tipLabel.textColor = (len >= 0) ? UIColor.lightGrayColor() : UIColor.redColor()
    }
    
    // MARK: - 私有控件
    /// 占位标签
    private lazy var placeholderLabel = UILabel(
        cz_text: "分享新鲜事...",
        fontSize: 18,
        color: UIColor.lightGrayColor())
    /// 长度提示标签
    private lazy var tipLabel = UILabel(
        cz_text: "",
        fontSize: 12,
        color: UIColor.lightGrayColor())
}

// MARK: - 设置界面
extension ComposeTextView {
    
    private func setupUI() {
        // 设置参数
        textColor = UIColor.darkGrayColor()
        font = UIFont.systemFontOfSize(18)
        
        // 始终允许垂直滚动
        alwaysBounceVertical = true
        // 拖动解除键盘
        keyboardDismissMode = .OnDrag
        
        // 0> 设置 size 视图
        // scrollView 中要使用自动布局，必须使用一个 view 设置 contentSize
        // a> edges 设置边距
        // b> size 设置 contentSize
        let sizeView = UIView()
        
        //禁用 sizeView 的用户交互，默认是允许用户交互的
        sizeView.userInteractionEnabled = false
        
        insertSubview(sizeView, atIndex: 0)
        
        sizeView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
            make.size.equalTo(self)
        }
        
        // 1> 添加占位标签
        addSubview(placeholderLabel)
        
        let margin: CGFloat = 5
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(8)
            make.left.equalTo(self).offset(margin)
            
            // textView 是 scrollView 的子视图
            // make.right.equalTo(self).offset(-5)
            make.width.equalTo(self).offset(-2 * margin)
        }
        
        // 2> 添加长度提示标签
        addSubview(tipLabel)
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self).offset(-margin)
            make.right.equalTo(self).offset(-margin)

        }
    }
}

